﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Script.Serialization;
using System.Net;
using System.IO;
using AutorizacionViajesMVC.Models;
using System.Text;

namespace AutorizacionViajesMVC.Controllers
{
    public class SeguroController : Controller
    {
        private string seguroRESTService = "http://localhost:13005/Seguros.svc/Seguros";
        JavaScriptSerializer js = new JavaScriptSerializer();
       

        public ActionResult Index()
        {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(seguroRESTService);
            req.Method = "GET";
            HttpWebResponse res = (HttpWebResponse)req.GetResponse();
            StreamReader reader = new StreamReader(res.GetResponseStream());
            string seguroJson = reader.ReadToEnd();
            List<Seguro> seguros = js.Deserialize<List<Seguro>>(seguroJson);
            return View(seguros);
            //return View();
        }

        //
        // GET: /Seguro/Details/5

        public ActionResult Details(string id)
        {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(seguroRESTService + "/" + id);
            req.Method = "GET";
            HttpWebResponse res = (HttpWebResponse)req.GetResponse();
            StreamReader reader = new StreamReader(res.GetResponseStream());
            string seguroJson = reader.ReadToEnd();
            Seguro seguro = js.Deserialize<Seguro>(seguroJson);
            return View(seguro);
        }

        //
        // GET: /Seguro/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Seguro/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            //try
            //{
            //    // TODO: Add insert logic here

            //    return RedirectToAction("Index");
            //}
            //catch
            //{
            //    return View();
            //}

            try
            {
                string postdata = "{\"CodigoS\":\"" + collection["CodigoS"] + "\",\"CodAU\":\"" + collection["CodAU"] + "\",\"Apro\":\"" + collection["Apro"] + "\",\"OB_Ge\":\"" + collection["OB_Ge"] + "\",\"VB_Teso\":\"" + collection["VB_Teso"] + "\",\"OB_Teso\":\"" + collection["OB_Teso"] + "\",\"VB_Seg\":\"" + collection["VB_Seg"] + "\",\"OB_Seg\":\"" + collection["OB_Seg"] + "\",\"EstaSol\":\"" + collection["EstaSol"] + "\"}"; 
                byte[] data = Encoding.UTF8.GetBytes(postdata);
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(seguroRESTService);
                req.Method = "POST";
                req.ContentLength = data.Length;
                req.ContentType = "application/json";
                var reqStream = req.GetRequestStream();
                reqStream.Write(data, 0, data.Length);
                var res = (HttpWebResponse)req.GetResponse();
                return RedirectToAction("Index");
            }
            catch (WebException e)
            {
                HttpWebResponse resError = (HttpWebResponse)e.Response;
                StreamReader reader2 = new StreamReader(resError.GetResponseStream());
                string error = reader2.ReadToEnd();
                JavaScriptSerializer js1 = new JavaScriptSerializer();
                Error errorMessage = js1.Deserialize<Error>(error);
                //ModelState.AddModelError("Error", e.Message);
                TempData["error"] = errorMessage.MensajeNegocio;
                return View("Create");
                //return RedirectToAction("Index");
            }
        }
        
        //
        // GET: /Seguro/Edit/5
 
        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /Seguro/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Seguro/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Seguro/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
